import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-08-05
 * Time: 14:37
 */
public class MyArrayList implements IList{
    public int[] array;
    public int useSize;

    public static final int DEAFUL_CAPACITY = 10;

    public MyArrayList(){
        this.array = new int[DEAFUL_CAPACITY];
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }

        this.array[useSize] = data;
        useSize++;
    }

    public boolean isFull(){
        return this.useSize == array.length;
    }

    private void grow(){
        array  = Arrays.copyOf(array, 2*array.length);
    }


    public void checkPos(int pos) throws PosIllegal{
        if(pos < 0 || pos >useSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }

    @Override
    public void add(int pos, int data) {
        try{
            checkPos(pos);
        }catch (PosIllegal e){
            System.out.println("输入元素pos位置不合法！！！");
            e.printStackTrace();
        }

        if(isFull()){
            grow();
        }
        for (int i = pos; i <= useSize ; i++) {
            array[i+1] = array[i];
        }
        useSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(array[i] == toFind){
                return i;
            }
        }
        return 0;
    }


    public void checkPos2(int pos) throws PosIllegal{
        if(pos < 0 || pos >= useSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }

    private boolean isEmpty(){
        return useSize == 0;
    }

    public void checkEmpty()throws EmptyException{
        if(isEmpty()){
            throw new EmptyException("数组为空数组！！！");
        }
    }
    // 获取 pos 位置的元素
    @Override
    public int get(int pos) {
        try{
            checkPos2(pos);
            checkEmpty();
            return array[pos];
        }catch (PosIllegal e){
            System.out.println("输入元素pos位置不合法！！！");
            e.printStackTrace();
        }catch (EmptyException e){
            System.out.println("数组为空数组！！！");
            e.printStackTrace();
        }

        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkPos2(pos);
            checkEmpty();
            array[pos] = value;
        }catch (PosIllegal e){
            System.out.println("输入元素pos位置不合法！！！");
            e.printStackTrace();
        }catch (EmptyException e){
            System.out.println("数组为空数组！！！");
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos = indexOf(toRemove);
            if(pos == -1){
                return;
            }
            for (int i = pos; i <useSize - 1 ; i++) {
                array[i] = array[i+1];
            }
            useSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
        useSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(array[i] + " ");
        }
    }
}
